import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { FactorySchedule, FactoryDay } from "@/api/backend/index";
import { handleTree, handleRegionIds } from "@/utils/common";

export const useFactorySchedule = defineStore(Names.factorySchedule, {
  state: () => {
    return {
      single: {},
      equipmentScheduleInfo: {},
      parentScheduleNode: [],
      backupSearch: {},
    };
  },
  getters: {},
  actions: {
    //工厂日历导入excel文件
    importExcel(data: any) {
      return new Promise((resolve, reject) => {
        _post(FactorySchedule + "/import", data, "formData", "")
          .then((res) => {
            console.log("导入excel文件", res);
            resolve(res as any);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    //获取工厂日历数据
    getScheduleList() {
      return new Promise((resolve, reject) => {
        _get(FactorySchedule + "/list", {}, "json", "")
          .then(async (res: any) => {
            resolve(res.rows);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    //获取工厂日历记录数据
    getScheduleRecordList(filter: any) {
      return new Promise((resolve, reject) => {
        _get(FactoryDay + "/list", filter, "json", "")
          .then(async (res: any) => {
            console.log("获取工厂日历记录数据", res);
            resolve(res.rows);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    //修改工厂日历记录数据
    editScheduleRecord(data: any) {
      return new Promise((resolve, reject) => {
        _put(FactoryDay, data, "json", "")
          .then(async (res: any) => {
            console.log("修改工厂日历记录数据", res);
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    //删除工厂日历记录
    deleteScheduleRecord(data: any) {
      return new Promise((resolve, reject) => {
        _delete(FactoryDay, data, "json", "")
          .then(async (res: any) => {
            console.log("删除工厂日历记录", res);
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
  },
});
